package top.lvkun.org.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.lvkun.basic.constant.Constant;
import top.lvkun.basic.service.impl.BaseServiceImpl;
import top.lvkun.basic.utils.AjaxResult;
import top.lvkun.basic.utils.MD5Utils;
import top.lvkun.basic.utils.MailUtils;
import top.lvkun.basic.utils.StrUtils;
import top.lvkun.org.domain.Employee;
import top.lvkun.org.domain.Shop;
import top.lvkun.org.mapper.EmployeeMapper;
import top.lvkun.org.mapper.ShopMapper;
import top.lvkun.org.service.IShopService;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private MailUtils mailUtils;

    /**
     * 门店入驻，提交表单
     * @param shop
     * @return
     */
    @Override
    @Transactional
    public AjaxResult settledIn(Shop shop) {
        //1.先保存t_employee表的数据，返回主键，因为t_shop表中有一个admin_id外键列 关联员工表的主键
        Employee admin = shop.getAdmin();
        admin.setSalt(StrUtils.getComplexRandomString(32));
        admin.setPassword(MD5Utils.encrypByMd5(admin.getPassword()+admin.getSalt()));
        employeeMapper.save(admin);

        //2.保存shop信息，并返回主键id
        shopMapper.save(shop);

        //3.修改管理员的店铺id为当前shop的id
        admin.setShop_id(shop.getId());
        System.out.println(admin.getShop_id());
        employeeMapper.update(admin);
        return AjaxResult.me();
    }

    /**
     * 审核门店入驻信息，提交表单
     * @param id
     * @param type
     */
    @Override
    @Transactional
    public void shopAudit(Long id, Integer type) {
        //根据id查询店铺对象
        Shop shop = shopMapper.loadById(id);
        switch (type){
            case 1: break;
            //驳回
            case 2:
                //发送通知邮件
                String content2 = "<h2 style='color:red;'>抱歉您的店铺信息审核未通过！</h2><br/><br/>"
                        + "请点击以下链接重新填写信息！<br/><br/>" +
                        "<a href='http://172.16.80.248:8081/#/settleIn?id="+id+"'>点击激活</a>";
                //修改门店及员工状态
                setState(shop,"审核失败",Constant.STATE_FORBIDDEN,content2);
                System.out.println(shop.getName() + "的审核操作完成！");
                break;
            case 3:
                //发送激活邮件
                String content3 = "<h2 style='color:red;'>恭喜你成功入驻宠物之家平台！</h2><br/><br/>"
                        + "点击以下链接即可激活！<br/><br/>" +
                        "<a href='http://172.16.80.248:8080/lr/shopRegActive/"+id+"'>点击激活</a>";
                setState(shop,"店铺入驻激活",Constant.STATE_ACTIVE,content3);
                System.out.println(shop.getName() + "的审核操作完成！");
                break;
        }

    }

    public void setState(Shop shop,String theme,Integer state,String content){
        //修改店铺的状态
        shop.setState(state);
        shopMapper.update(shop);
        //修改店铺关联的员工对象的状态
        Employee admin = employeeMapper.loadById(shop.getAdmin_id());
        admin.setState(state);
        employeeMapper.update(admin);
        //发送激活邮件
        mailUtils.send(admin.getEmail(),theme,content);
    }

    /**
     * 用户点击邮箱链接，激活门店和个人账号
     * @param id
     */
    @Override
    @Transactional
    public void shopRegActive(Long id) {
        //激活门店
        Shop shop = shopMapper.loadById(id);
        shop.setState(Constant.STATE_NORMAL);
        shopMapper.update(shop);

        //激活个人账号
        Employee admin = employeeMapper.loadById(shop.getAdmin_id());
        admin.setState(Constant.STATE_NORMAL);
        employeeMapper.update(admin);
    }
}
